package com.example.myapplication.module1.activity

import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R
import com.example.myapplication.databinding.ActivityChooseCustomerBinding
import com.example.myapplication.entity.CustomerInfoBean
import com.example.myapplication.module1.adapter.CustomerAdapter
import com.example.myapplication.module1.presenter.ChooseCustomerPresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.CustomerRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.adapter.BaseRecyclerAdapter
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.UiTools
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

/**
 * 选择客户
 */
class ChooseCustomerActivity :
    BaseMvpActivity<ChooseCustomerActivity, ChooseCustomerPresenter, ActivityChooseCustomerBinding>() {
    lateinit var loadPopup: LoadingPopupView
    private val tempCustomerInfoList = mutableListOf<CustomerInfoBean>()
    private val customerRequest = CustomerRequest()
    private val soapRequestBody = SoapRequestBody()
    private val soapEnvelop = SoapEnvelop()
    var curPage = 1

    private val customerAdapter: CustomerAdapter by lazy {
        CustomerAdapter(this)
    }

    override fun getPresenter(): ChooseCustomerPresenter = ChooseCustomerPresenter()

    override fun getBinding(): ActivityChooseCustomerBinding {
        return ActivityChooseCustomerBinding.inflate(layoutInflater)
    }

    override fun initData() {
        getCustomerInfo()
    }

    private fun getCustomerInfo() {
        if (curPage == 1) {
            tempCustomerInfoList.clear()
        }
        customerRequest.curpage = "$curPage"
        customerRequest.types = "1"
        customerRequest.filter = viewBinding.etSearch.text.toString()
        soapRequestBody.value = customerRequest
        soapEnvelop.body = soapRequestBody
        mPresenter?.getCustomer(soapEnvelop)
    }

    override fun initView() {
        loadPopup = builder.asLoading("加载中...")

        viewBinding.list.rvList.addItemDecoration(
            DividerItemDecoration(
                this,
                RecyclerView.VERTICAL
            )
        )
        viewBinding.list.rvList.adapter = customerAdapter
        customerAdapter.clickListener = object : BaseRecyclerAdapter.ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val customerInfoList = customerAdapter.customerInfoList
                val customerInfoBean = customerInfoList[position]
                val intent = Intent()
                intent.putExtra("customerInfoBean", customerInfoBean)
                setResult(RESULT_OK, intent)
                finish()
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
                return false
            }
        }

        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {
                // 新增

            }
        }
        viewBinding.etSearch.setOnEditorActionListener(object : TextView.OnEditorActionListener {
            override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
                if ((actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_NEXT
                            || (event != null && event.keyCode == KeyEvent.KEYCODE_ENTER))
                ) {
                    curPage = 1
                    getCustomerInfo()
                    return true
                }
                return false
            }
        })

        viewBinding.list.srlRefresh.setOnRefreshLoadMoreListener(object :
            OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                curPage = 1
                getCustomerInfo()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                curPage += 1
                getCustomerInfo()
            }
        })
    }

    override fun showLoading() {
        if (!viewBinding.list.srlRefresh.isRefreshing && !viewBinding.list.srlRefresh.isLoading) {
            loadPopup.show()
        }
    }

    override fun showSuccess() {
        loadPopup.dismiss()
        if (viewBinding.list.srlRefresh.isRefreshing || viewBinding.list.srlRefresh.isLoading) {
            viewBinding.list.srlRefresh.finishLoadMore()
            viewBinding.list.srlRefresh.finishRefresh()
        }
    }

    fun setCustomer(result: String) {
        val customerInfoBeans = GsonUtil.fromJson<MutableList<CustomerInfoBean>>(
            result,
            object : TypeToken<MutableList<CustomerInfoBean>>() {}.type
        )
        if (customerInfoBeans.isNullOrEmpty()) {
            viewBinding.list.srlRefresh.setEnableLoadMore(false)
            UiTools.showToast(R.string.noMoreInfo)
        } else {
            viewBinding.list.srlRefresh.setEnableLoadMore(true)
            tempCustomerInfoList.addAll(customerInfoBeans)
        }
        customerAdapter.customerInfoList = tempCustomerInfoList

        if (tempCustomerInfoList.size > 0) {
            viewBinding.noData.clNoData.visibility = View.GONE
        } else {
            viewBinding.noData.clNoData.visibility = View.VISIBLE
        }

    }
}
